//
// Created by Administrator on 2023/5/28.
//
#pragma once
#ifndef WIDGET_MOVE_H
#define WIDGET_MOVE_H


#include <QMouseEvent>

template <typename Owner>
class Widget_Move
{
public:
    Widget_Move(Owner* owner);


    ~Widget_Move();


    void mouseMoveEvent(QMouseEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);


private:
    Widget_Move();


    Owner* owner;
    bool press {false};
    QPoint pos_start;
    QPoint topleft_pos_start;
};


template <typename Owner>
Widget_Move<Owner>::Widget_Move(Owner* owner):
        owner(owner){}


template <typename Owner>
void Widget_Move<Owner>::mouseMoveEvent(QMouseEvent* event)
{
    if(press)
    {
        QPoint end_pos = event->globalPos() - this->pos_start;
        this->owner->move(this->topleft_pos_start + end_pos);
    }
}


template <typename Owner>
void Widget_Move<Owner>::mousePressEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton)
    {
        this->press = true;
        this->pos_start = event->globalPos();
        this->topleft_pos_start = this->owner->geometry().topLeft();
    }
}


template <typename Owner>
void Widget_Move<Owner>::mouseReleaseEvent(QMouseEvent* event)
{
    this->press = false;
}






#endif //WIDGET_MOVE_H
